﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RMS.Models;
using RMS.Models.Repository;
using System.Web.ModelBinding;
using System.Data.Entity;
using RMS.Pages.Helpers;
using System.Web.Routing;

namespace RMS.Pages
{
    public partial class Search : System.Web.UI.Page
    {
        private Repository repo = new Repository();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {

                int selectedRetreatId;
                if (int.TryParse(Request.Form["add"], out selectedRetreatId))
                {

                    Retreat selectedRetreat = repo.Retreats
                    .Where(p => p.Ret_ID == selectedRetreatId).FirstOrDefault();

                    if (selectedRetreat != null)
                    {
                        SessionHelper.GetCart(Session).AddItem(selectedRetreat, 1);
                        SessionHelper.Set(Session, SessionKey.RETURN_URL,
                        Request.RawUrl);
                        Response.Redirect(RouteTable.Routes
                        .GetVirtualPath(null, "cart", null).VirtualPath);
                    }
                }
            }
        }

        public IEnumerable<Retreat> GetRetreats()
        {
            string selectedCategory = cbCatSelect.Value;
            if (selectedCategory.Equals("All Categories"))
            {
                return repo.Retreats
                .OrderBy(p => p.Ret_Title);
            }
            else
            {
                return repo.Retreats
                .OrderBy(p => p.Ret_Title)
                .Where((p => (p.Ret_Main_Category == selectedCategory) || (p.Ret_Category_Two == selectedCategory)
                                                                       || (p.Ret_Category_Three == selectedCategory) || (p.Ret_Category_Four == selectedCategory)));
            }

        }

        // Formats dates for retreat listings (utilizes | as delimeter)
        public string dateFormat(string input)
        {
            string str = input;
            Boolean notValid = true;
            int num;
            int count = 0;
            string temp = "";

            while (notValid)
            {

                num = str.IndexOf('|', 0);

                if (num == -1)
                {
                    if (count == 0)
                    {
                        temp = "<h5>" + str + "</h5>";
                    }
                    else
                    {
                        temp += "<h5>" + str + "</h5>";
                    }

                    notValid = false;
                }
                else
                {
                    temp += "<h5>" + str.Substring(0, num) + "</h5>";
                    str = str.Substring(num + 1);
                }

                count++;
            }

            return temp;
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Repeater1.DataBind();
        }

    }
}